### Preliminary ----------------------------------------------------------------
# Load library
library(tidyverse)

# Load data
house <- read_csv("House Elections (Jacobson-Algara).csv")
cook_2020 <- read_csv("Cook PVI (2020).csv")

### Creating Figure 1 ----------------------------------------------------------
# Reshape data for graphing
graphing_df <- house %>%
  arrange(district, year) %>%
  group_by(year) %>%
  mutate(dpres_demeaned = dpres - mean(dpres)) %>%
  group_by(district) %>%
  mutate(dpres_lagged = lag(dpres_demeaned),
         dpres_lagged = ifelse(year %in% seq(1972, 2012, 10), NA, dpres_lagged)) %>%
  ungroup() %>%
  mutate(cook_pvi = ifelse(is.na(dpres_lagged), dpres_demeaned,
                           (dpres_demeaned + dpres_lagged) / 2)) %>%
  bind_rows(cook_2020) %>%
  group_by(year) %>%
  summarize(num_Competitive = sum(cook_pvi > -5 &
                                    cook_pvi < 5),
            num_Safe = 435 - num_Competitive) %>%
  pivot_longer(cols = c(num_Competitive, num_Safe), names_to = "type",
               names_prefix = "num_",
               values_to = "NumberSeats")

# Create plot
ggplot(graphing_df, aes(x = year, y = NumberSeats, lty = type)) +
  geom_line(size = 1) +
  geom_point(data = data.frame(year = c(2020, 2020),
                               NumberSeats = c(71, 364),
                               type = c("Competitive", "Safe")), size = 2) +
  geom_rect(aes(xmin = 1999, xmax = 2021, ymin = 60, ymax = 420),
            color = "gray", alpha = 0.01) +
  scale_y_continuous(limits = c(60, 420), expand = c(0, 0),
                     breaks = seq(100, 400, 100)) +
  scale_x_continuous(limits = c(1974, 2021), expand = c(0, 0)) +
  labs(x = NULL, y = "Number of U.S. House Seats", lty = "Seat Type") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    legend.position = c(0.15, 0.85),
    legend.background = element_rect(color = "black")
  )

